package com.news.parser;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class NewsContentHandler extends DefaultHandler{

	public static ArrayList<Data> dataList = new ArrayList<Data>();
	public Data data = null;
	
	private class Data
	{
		String name,address,salary,sex,status;
		String tagName;
		
		public String getName()
		{
			return name;
		}
		public void setName(String name)
		{
			this.name = name;
		}
		
		public String getAddress()
		{
			return address;
		}
		public void SetAddress(String address)
		{
			this.address = address;
		}
	}
	
	
	private String tagName;
	
	public ArrayList getParseredData()
	{
		return dataList;
	}
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if(tagName.equals("name"))
			data.setName( new String(ch, start, length) );
		/*
		else if(tagName.equals("sex"))
			sex = new String(ch,start,length);
		else if(tagName.equals("status"))
			status = new String(ch,start,length);
		else if(tagName.equals("money"))
			address = new String(ch,start,length);
			*/
		else if(tagName.equals("address"))
			 data.SetAddress(new String(ch,start,length));
	}

	@Override
	public void endDocument() throws SAXException {
		System.out.println("--------------end-------------");
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		tagName = "";
		if(localName.equals("worker"))
		{
			dataList.add( data );
		}
	}

	@Override
	public void startDocument() throws SAXException {
		System.out.println("--------------start-------------");
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		tagName = localName;
		if(localName.equals("worker"))
		{
			/*
			for(int i=0; i<attributes.getLength(); i++)
			{
				System.out.println(attributes.getLocalName(i)+"="+attributes.getValue(i));
			}
			*/
			data = new Data();
		}
	}

}
